package com.scaffold.myproject.web.controller.item;

import com.scaffold.myproject.common.PageResult;
import com.scaffold.myproject.common.Result;
import com.scaffold.myproject.entity.item.ItemDoc;
import com.scaffold.myproject.entity.item.ItemEntity;
import com.scaffold.myproject.model.req.item.ItemPageReq;
import com.scaffold.myproject.service.item.ItemService;
import com.scaffold.myproject.utils.EsPageQueryUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Map;

/**
 * @author lx
 * @date 2025/10/30
 * @Description
 */
@RestController
@RequestMapping("/item")
@Slf4j
@ApiModel("商品管理")
public class ItemController {

    @Resource
    private ItemService itemService;

    /**
     * 耗时 150ms 左右
     */
    @ApiOperation("分页查询商品（基于数据库）")
    @PostMapping("/page/db")
    public Result<PageResult<ItemEntity>> pageQueryDb(@RequestBody ItemPageReq req) {
        log.info("分页查询商品（DB）: {}", req);
        PageResult<ItemEntity> result = itemService.searchPageFromDb(req);
        return Result.of(result);
    }

    /**
     * 耗时 25ms左右
     */
    @ApiOperation("分页查询商品（基于 Elasticsearch）")
    @PostMapping("/page")
    public Result<PageResult<ItemDoc>> pageQuery(@RequestBody ItemPageReq req) throws IOException {
        log.info("分页查询商品: {}", req);
        PageResult<ItemDoc> result =itemService.searchPage(req);
        return Result.of(result);
    }
}
